//
//  GesturePasswordView.m
//  GesturePassword
//
//  Created by hb on 14-8-23.
//  Copyright (c) 2014年 黑と白の印记. All rights reserved.
//

// 版权属于原作者
// http://code4app.com (cn) http://code4app.net (en)
// 发布代码于最专业的源码分享网站: Code4App.com

#import "GesturePasswordView.h"
#import "GesturePasswordButton.h"
#import "TentacleView.h"
#import "MicroDef.h"
#import "UIUtil.h"
@implementation GesturePasswordView {
    NSMutableArray * buttonArray;
    
    CGPoint lineStartPoint;
    CGPoint lineEndPoint;
    NSMutableArray *littleButtonArray;
    
}

@synthesize forgetButton;
@synthesize changeButton;

@synthesize tentacleView;
@synthesize state;
@synthesize gesturePasswordDelegate;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        
        CGFloat padding1 = 0;
        CGFloat padding2 = 0;
        CGFloat padding3 = 0;
        CGFloat padding4 = 0;
        CGFloat stateFontSize = 0;
        CGFloat errMsgFontSize = 0;
        CGFloat reloginFontSize = 0;
        CGFloat ImageSize = 85.0f;
        CGFloat viewSize = 260.f;
        if(HARDWARE_SCREEN_HEIGHT <= 480)
        {
            padding1 = 30;
            padding2 = 15;
            padding3 = 45;
            padding4 = 25;
            stateFontSize = 15;
            errMsgFontSize = 12;
            reloginFontSize = 14;
        }
        else if(HARDWARE_SCREEN_HEIGHT <= 568)
        {
            padding1 = 45;
            padding2 = 20;
            padding3 = 55;
            padding4 = 45;
            stateFontSize = 15;
            errMsgFontSize = 12;
            reloginFontSize = 14;
        }
        else if(HARDWARE_SCREEN_HEIGHT <= 736)
        {
            padding1 = 260/3.0f;
            padding2 = 100/3.0f;
            padding3 = 215/3.0f;
            padding4 = 160/3.0f;
            stateFontSize = 22;
            errMsgFontSize = 18;
            reloginFontSize = 21;
            viewSize = 320.0f;
        }
//        NSLog(@"HARDWARE_SCREEN_HEIGHT:%f", HARDWARE_SCREEN_HEIGHT);
        
        
        
        
        
        // Initialization code
        buttonArray = [[NSMutableArray alloc]initWithCapacity:0];
        littleButtonArray = [[NSMutableArray alloc]initWithCapacity:0];
        NSInteger distance = viewSize/3;
        NSInteger size = distance/1.5;
        NSInteger margin = size/4;
        UIView * view = [[UIView alloc]initWithFrame:CGRectMake(frame.size.width/2-viewSize/2, padding1+padding2+padding3+ImageSize, viewSize, viewSize)];
//        view.backgroundColor = [UIColor greenColor];
        for (int i=0; i<9; i++) {
            NSInteger row = i/3;
            NSInteger col = i%3;
            // Button Frame
            
//            NSInteger distance = 320/3;

            GesturePasswordButton * gesturePasswordButton = [[GesturePasswordButton alloc]initWithFrame:CGRectMake(col*distance+margin, margin+ row*distance, size, size)];
//            gesturePasswordButton.layer.borderColor = [UIColor redColor].CGColor;
//            gesturePasswordButton.layer.borderWidth = 2.0f;
            [gesturePasswordButton setTag:i];
            [view addSubview:gesturePasswordButton];
//            gesturePasswordButton.backgroundColor = [UIColor greenColor];
            [buttonArray addObject:gesturePasswordButton];
        }
        
        
        frame.origin.y=0;
//        view.backgroundColor = [UIColor redColor];
        [self addSubview:view];
        tentacleView = [[TentacleView alloc]initWithFrame:view.frame];
        [tentacleView setButtonArray:buttonArray];
        [tentacleView setTouchBeginDelegate:self];
        [tentacleView setTouchEndDelegate:self];
        [self addSubview:tentacleView];
        
//        state = [[UILabel alloc]initWithFrame:CGRectMake(frame.size.width/2-140, frame.size.height/2-120, 280, 25)];
        state = [[UILabel alloc]initWithFrame:CGRectMake(frame.size.width/2-140, padding1+padding2+85, 280, 25)];
        [state setTextAlignment:NSTextAlignmentCenter];
        [state setFont:[UIFont systemFontOfSize:stateFontSize]];
        [state setTextColor:RGBCOLOR(0x32, 0x32, 0x32)];
//        [state setBackgroundColor:[UIColor redColor]];
        [self addSubview:state];
        _errorMsg = [[UILabel alloc]initWithFrame:CGRectMake(frame.size.width/2-160, state.frame.origin.y+state.frame.size.height, 320, 25)];
        [_errorMsg setTextAlignment:NSTextAlignmentCenter];
        [_errorMsg setFont:[UIFont systemFontOfSize:errMsgFontSize]];
        [_errorMsg setTextColor:RGBCOLOR(0x7c, 0x7c, 0x7c)];
        [_errorMsg setText:@"3次密码输入失败后, 您需要重新登录"];
//        [_errorMsg setBackgroundColor:[UIColor greenColor]];
        [self addSubview:_errorMsg];
        [_errorMsg setHidden:YES];
        
        
        UIView *guesView = [[UIView alloc] initWithFrame:CGRectMake(frame.size.width/2-42.5, padding1, ImageSize, ImageSize)];
        guesView.backgroundColor = [UIColor clearColor];
        [self addSubview:guesView];
        _guesView = guesView;
        _guesView.hidden = YES;
        
        for (int i=0; i<9; i++) {
            NSInteger row = i/3;
            NSInteger col = i%3;
            // Button Frame
            
            //            NSInteger distance = 320/3;
            
            UIButton * guesButton = [[UIButton alloc]initWithFrame:CGRectMake(col*(25+5),row*(25+5), 25, 25)];
            [guesButton setImage:[UIImage imageNamed:@"little-unClick"] forState:UIControlStateNormal];
            [guesButton setImage:[UIImage imageNamed:@"little-click"] forState:UIControlStateSelected];
            guesButton.userInteractionEnabled = NO;
            [guesButton setTag:i];
            
            [littleButtonArray addObject:guesButton];
            [view addSubview:guesButton];
            [guesView addSubview:guesButton];
        }

        
        _imgView = [[UIImageView alloc]initWithFrame:CGRectMake(frame.size.width/2-42.5, padding1, ImageSize, ImageSize)];
        [_imgView setBackgroundColor:[UIColor clearColor]];
//        [imgView.layer setCornerRadius:35];
//        [imgView.layer setBorderColor:[UIColor grayColor].CGColor];
//        [imgView.layer setBorderWidth:3];
        [_imgView setImage:[UIImage imageNamed:@"logoBackgroundImage"]];
        [self addSubview:_imgView];
        _imgView.hidden = YES;
        
//        _headerImageView = [[UIImageView alloc]initWithFrame:CGRectMake(frame.size.width/2-42.5, 30, 85, 85)];
        _headerImageView = [[UIImageView alloc]initWithFrame:CGRectMake(_imgView.frame.origin.x, _imgView.frame.origin.y, 85, 85)];
        [_headerImageView setBackgroundColor:[UIColor clearColor]];
        _headerImageView.layer.cornerRadius = 42.5f;
        _headerImageView.layer.masksToBounds = YES;
        //        [imgView.layer setCornerRadius:35];
        //        [imgView.layer setBorderColor:[UIColor grayColor].CGColor];
        //        [imgView.layer setBorderWidth:3];
        [_headerImageView setImage:[UIImage imageNamed:@"defaultHeaderImage"]];
        [self addSubview:_headerImageView];

        _headerImageView.hidden = YES;
        
        forgetButton = [[UIButton alloc]initWithFrame:CGRectMake(frame.size.width/2-60, frame.size.height- 50, 120, 30)];
        [forgetButton.titleLabel setFont:[UIFont systemFontOfSize:14]];
        [forgetButton setTitleColor:RGBCOLOR(0x2c, 0xaa, 0xfe) forState:UIControlStateNormal];
        [forgetButton setTitle:@"重新登录" forState:UIControlStateNormal];
        [forgetButton addTarget:self action:@selector(forget) forControlEvents:UIControlEventTouchDown];
        
         [self addSubview:forgetButton];
       
//        
//        changeButton = [[UIButton alloc]initWithFrame:CGRectMake(frame.size.width/2-60, frame.size.height - 50, 120, 30)];
//        [changeButton.titleLabel setFont:[UIFont systemFontOfSize:14]];
//        [changeButton setTitleColor:RGBCOLOR(0x2c, 0xaa, 0xfe) forState:UIControlStateNormal];
//        [changeButton setTitle:@"取消手势密码" forState:UIControlStateNormal];
//        [changeButton addTarget:self action:@selector(change) forControlEvents:UIControlEventTouchDown];
//        [self addSubview:changeButton];
        
//        reLoginButton
//        _reLoginButton = [[UIButton alloc]initWithFrame:CGRectMake(frame.size.width/2 - 120/2.0f, frame.size.height/2+220, 120, 30)];
        _reLoginButton = [[UIButton alloc]initWithFrame:CGRectMake(frame.size.width/2 - 120/2.0f, view.frame.origin.y+view.frame.size.height+padding4-margin, 120, 30)];
        [_reLoginButton.titleLabel setFont:[UIFont systemFontOfSize:reloginFontSize]];
        [_reLoginButton setTitleColor:RGBCOLOR(0x2c, 0xaa, 0xfe) forState:UIControlStateNormal];
        [_reLoginButton setTitle:@"重新登录" forState:UIControlStateNormal];
        _reLoginButton.hidden = YES;
        [_reLoginButton addTarget:self action:@selector(change) forControlEvents:UIControlEventTouchDown];
        [self addSubview:_reLoginButton];
    }
    
    return self;
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect
{
    // Drawing code
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();
//    CGFloat colors[] =
//    {
//        134 / 255.0, 157 / 255.0, 147 / 255.0, 1.00,
//        3 / 255.0,  3 / 255.0, 37 / 255.0, 1.00,
//    };
    CGFloat colors[] =
    {
        0xee / 255.0, 0xf9 / 255.0, 0xff / 255.0, 1.00,
        0xee / 255.0, 0xf9 / 255.0, 0xff / 255.0, 1.00,
    };
    
    CGGradientRef gradient = CGGradientCreateWithColorComponents
    (rgb, colors, NULL, sizeof(colors)/(sizeof(colors[0])*4));
    CGColorSpaceRelease(rgb);
    CGContextDrawLinearGradient(context, gradient,CGPointMake
                                (0.0,0.0) ,CGPointMake(0.0,self.frame.size.height),
                                kCGGradientDrawsBeforeStartLocation);
}


- (void)gestureTouchEnd:(NSArray *)selectedArray
{
    [littleButtonArray enumerateObjectsUsingBlock:^(UIButton *obj, NSUInteger idx, BOOL *stop) {
        
        obj.selected = NO;
        for (NSDictionary *dict in selectedArray) {
            if (obj.tag == [[dict objectForKey:@"num"] integerValue]) {
                obj.selected = YES;
            }
        }
    }];
}

- (void)gestureTouchBegin {

    
}

-(void)forget{
    [gesturePasswordDelegate forget];
}

-(void)change{
    [gesturePasswordDelegate change];
}


@end
